import collections


class Solution(object):
    def numSimilarGroups(self, strs):
        n = len(strs)
        m = len(strs[0])

        def judge(i, j):
            word1 = strs[i]
            word2 = strs[j]
            c = 0
            for k in range(m):
                if word1[k] == word2[k]:
                    c += 1
            return c == m or c == m - 2

        self.ans = n
        p = [i for i in range(n)]

        def find(x):
            if p[x] == x:
                return x
            else:
                p[x] = find(p[x])
                return p[x]

        def union(x, y):
            rootx = find(x)
            rooty = find(y)
            if rootx != rooty:
                if rootx < rooty:
                    p[rooty] = rootx
                else:
                    p[rootx] = rooty
                self.ans -= 1

        for i in range(n):
            for j in range(i, n):
                if judge(i, j):
                    union(i, j)

        return self.ans


data = Solution()
strs = ["tars", "rats", "arts", "star"]
print(data.numSimilarGroups(strs))
